// @Describe: 客户端扩展
package clientx

import (
	"github.com/spf13/viper"
	"google.golang.org/grpc"
	"strconv"
	"wormhole/protos/hole"
)

type Client struct {
	*grpc.ClientConn
	hole.HoleClient
}

func (self Client) Close() {
	_ = self.ClientConn.Close()
}

func NewLocalClient() (*Client, error) {
	port := viper.GetString("core.port")
	conn, err := grpc.Dial("localhost:"+port, grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	c := hole.NewHoleClient(conn)
	client := &Client{
		ClientConn: conn,
		HoleClient: c,
	}
	return client, nil
}

func NewRippleClient(ripple *hole.Ripple) (*Client, error) {
	conn, err := grpc.Dial(ripple.Ip+":"+strconv.Itoa(int(ripple.Port)), grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	c := hole.NewHoleClient(conn)
	client := &Client{
		ClientConn: conn,
		HoleClient: c,
	}
	return client, nil
}
